<?php
namespace Shop\Api;
use Common\Api\Api;
use Shop\Model\MemberConfigModel;

/**
 * Created by PhpStorm.
 * User: an
 * Date: 2015/9/11
 * Time: 11:43
 */
class MemberConfigApi extends Api{
    const ADD="Shop/MemberConfig/add";
	
	 const QUERY="Shop/MemberConfig/query";

    const SAVE_BY_ID="Shop/MemberConfig/saveByID";
	
	const SAVE="Shop/MemberConfig/save";

    const QUERY_NO_PAGING="Shop/MemberConfig/queryNoPaging";

    const GET_INFO = "Shop/MemberConfig/getInfo";

    const SET_INC="Shop/MemberConfig/setInc";

    const QUERY_ACCOUNT = "Shop/MemberConfig/queryAccount";

    const COUNT = "Shop/MemberConfig/count";

    protected function _init(){
        $this->model=new MemberConfigModel();
    }

    /**
     * 根据id保存数据
     * @param $ID
     * @param $entity
     * @return bool|\Common\Api\status
     */
    public function saveByID($ID, $entity) {
        unset($entity['uid']);

        return $this -> save(array('uid' => $ID), $entity);
    }


    public function queryAccount($map = null, $page = array('curpage'=>0,'size'=>10), $order = false, $params = false, $fields = false){
        $query = $this->model -> join("itboye_ucenter_member on itboye_member_config.uid=itboye_ucenter_member.id","left") -> join("common_member on itboye_member_config.uid=common_member.uid","left");
        if(!is_null($map)){
            $query = $query->where($map);
        }
        if(!($order === false)){
            $query = $query->order($order);
        }
        if(!($fields === false)){
            $query = $query->field($fields);
        }
        $list = $query -> page($page['curpage'] . ',' . $page['size']) -> select();


        if ($list === false) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }

        $count = $this -> model -> where($map) -> join("itboye_ucenter_member on itboye_member_config.uid=itboye_ucenter_member.id","left") -> join("common_member on itboye_member_config.uid=common_member.uid","left") -> count();
        // 查询满足要求的总记录数
        $Page = new \Think\Page($count, $page['size']);

        //分页跳转的时候保证查询条件
        if ($params !== false) {
            foreach ($params as $key => $val) {
                $Page -> parameter[$key] = urlencode($val);
            }
        }

        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page -> show();

        return $this -> apiReturnSuc(array("count" => $count, "list" => $list));
    }

    /**
     *
     * 根据用户经验来设置用户组
     * @param $uid
     */
    public function setMGroup($uid,$store_id,$exp){


    }

}